<html>
<head><meta charset="utf-8"><title>Signal handlers and Rust · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html">Signal handlers and Rust</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="184840923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/184840923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#184840923">(Jan 05 2020 at 09:12)</a>:</h4>
<p>I have a terminal application and it would like to track the terminal size. On Windows this is communicated with normal event polling, but with unix systems a change in terminal size sends a signal to your process. By default the signal is ignored, so you have to set a custom signal handler.</p>
<p>My question is, "does Rust have any specific rules about signal handlers? or is it just <em>follow the C rules</em>?"</p>
<p>Particularly, can the signal handler just write an AtomicBool for "you need to check the size again", which the main code then checks every "frame" of input gathering? What aromic ordering would be sufficient here to interact that data between the main code and the signal handler, Or would even just a static Cell&lt;bool&gt; be correct?</p>



<a name="184843788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/184843788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#184843788">(Jan 05 2020 at 10:47)</a>:</h4>
<p>a static Cell&lt;usize&gt; would certainly not be correct, for the same reasons a non-volatile global in C isn't (ordinary load-store optimizations of sequential programs can break the communication with a signal handler)</p>



<a name="184857464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/184857464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#184857464">(Jan 05 2020 at 17:39)</a>:</h4>
<p>Yes <code>AtomicBool</code> should be fine for communicating with a signal handler.</p>



<a name="184857527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/184857527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#184857527">(Jan 05 2020 at 17:40)</a>:</h4>
<p>In terms of ordering, you don't need anything since you are not transmitting any data other than the flag. So <code>Relaxed</code> is fine.</p>



<a name="184857551"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/184857551" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#184857551">(Jan 05 2020 at 17:41)</a>:</h4>
<p>If you need anything more (i.e. sending additional data about the signal through an <code>UnsafeCell</code> guarded by an atomic flag), then you would need to use <code>compiler_fence</code> on the main thread side to ensure the flag is checked before you access the protected data.</p>



<a name="184865917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/184865917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#184865917">(Jan 05 2020 at 21:30)</a>:</h4>
<p>Hmm, so as long as it's only the atomic in question, just basic reads and writes are fine? I could also use an AtomicU32 to store a <code>[u16; 2]</code> of the dimensions or something like that.</p>



<a name="184869165"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/184869165" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#184869165">(Jan 05 2020 at 22:57)</a>:</h4>
<p>Yea if you data is within the atomic itself then relaxed load/store is fine.</p>



<a name="184940237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/184940237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#184940237">(Jan 06 2020 at 19:58)</a>:</h4>
<p>&lt;<a href="https://github.com/Lokathor/vanadium/blob/master/src/bin/vanadium.rs" target="_blank" title="https://github.com/Lokathor/vanadium/blob/master/src/bin/vanadium.rs">https://github.com/Lokathor/vanadium/blob/master/src/bin/vanadium.rs</a>&gt; it seems to be working well</p>



<a name="185052940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185052940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185052940">(Jan 07 2020 at 20:45)</a>:</h4>
<p>I'd treat the signal handler like a separate thread (which it sounds like you're already doing)</p>



<a name="185053008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185053008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185053008">(Jan 07 2020 at 20:46)</a>:</h4>
<p><span class="user-mention" data-user-id="143274">@Amanieu</span> when using release/acquire atomics, I suppose no fence is needed? what's good enough for inter-thread communication should also suffice for communication with the signal handler?</p>



<a name="185053056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185053056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185053056">(Jan 07 2020 at 20:47)</a>:</h4>
<p>(I guess using full-spectrum atomics is overkill, but then I'd be surprised if signal handlers were perf-critical)</p>



<a name="185053235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185053235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185053235">(Jan 07 2020 at 20:49)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> For signal handlers you can use Acquire/Release but that's overkill. In practice you just need Relaxed + compiler_fence.</p>



<a name="185053329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185053329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185053329">(Jan 07 2020 at 20:50)</a>:</h4>
<p>Also you only need the fence on the main thread side, not the signal handler, since returning from the signal handler acts as an implicit fence.</p>



<a name="185053650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185053650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185053650">(Jan 07 2020 at 20:53)</a>:</h4>
<p>you need an acquire fence in the signal handler though I suppose, to avoid reordering the signal load and the data load?</p>



<a name="185053708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185053708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185053708">(Jan 07 2020 at 20:54)</a>:</h4>
<p>I find it much easier to treat these as separate threads than trying to optimize for how signal handlers differ from threads^^</p>



<a name="185054031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185054031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185054031">(Jan 07 2020 at 20:58)</a>:</h4>
<p>if both the handler and the main code use Relaxed, will that reach an "eventual" consistency? That is, assuming the main thread is very frequently checking the signal handler's AtomicBool, I'm comfortable with the handler running and then the main code not seeing that change on the very next loop, only some fuzzy number of loops later.</p>



<a name="185054116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185054116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185054116">(Jan 07 2020 at 20:59)</a>:</h4>
<p>Such as with this:<br>
main loop reads the flag before getting any other input: &lt;<a href="https://github.com/Lokathor/vanadium/blob/master/src/bin/vanadium.rs#L142" target="_blank" title="https://github.com/Lokathor/vanadium/blob/master/src/bin/vanadium.rs#L142">https://github.com/Lokathor/vanadium/blob/master/src/bin/vanadium.rs#L142</a>&gt;<br>
and the signal handler just flips the flag on every time it's triggered &lt;<a href="https://github.com/Lokathor/vanadium/blob/master/src/bin/vanadium.rs#L202" target="_blank" title="https://github.com/Lokathor/vanadium/blob/master/src/bin/vanadium.rs#L202">https://github.com/Lokathor/vanadium/blob/master/src/bin/vanadium.rs#L202</a>&gt;</p>



<a name="185054251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185054251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185054251">(Jan 07 2020 at 21:01)</a>:</h4>
<p>To be clear, the demo does run and work as intended, I'm just kinda unclear if it's an accidentally sort of working or if this is an expected sort of working.</p>



<a name="185054255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185054255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185054255">(Jan 07 2020 at 21:01)</a>:</h4>
<p>It works as expected.</p>



<a name="185054272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185054272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185054272">(Jan 07 2020 at 21:01)</a>:</h4>
<p>okay cool</p>



<a name="185054355"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185054355" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185054355">(Jan 07 2020 at 21:02)</a>:</h4>
<p>I've never ever touched C++ so a lot of the time when Rust says "it works kinda how C++ works because that's what LLVM does" it can be harder.</p>



<a name="185054407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185054407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185054407">(Jan 07 2020 at 21:03)</a>:</h4>
<p>(I hope that in the future we'll move towards a knowledge base that doesn't assume the user knows any previous programming languages.)</p>



<a name="185054765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185054765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185054765">(Jan 07 2020 at 21:07)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> You only need acquire/release if you need to transfer data outside the atomic. For example:</p>
<div class="codehilite"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">DATA</span>: <span class="kt">i64</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="k">static</span><span class="w"> </span><span class="n">FLAG</span>: <span class="nc">AtomicBool</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">AtomicBool</span>::<span class="n">new</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">sig_handler</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Release ensures FLAG is stored after DATA</span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">DATA</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">FLAG</span><span class="p">.</span><span class="n">store</span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="n">Ordering</span>::<span class="n">Release</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">main_thread</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Acquire ensures FLAG is loaded before DATA</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">FLAG</span><span class="p">.</span><span class="n">load</span><span class="p">(</span><span class="n">Ordering</span>::<span class="n">Acquire</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">DATA</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="185054821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185054821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185054821">(Jan 07 2020 at 21:07)</a>:</h4>
<p>This is the generic version where the signal handler is modeled as a separate thread.</p>



<a name="185055016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185055016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185055016">(Jan 07 2020 at 21:09)</a>:</h4>
<p>oh so they only matter if the atomic is a pseudo-lock sort of thing on some other non-atomic data?</p>



<a name="185055050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185055050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185055050">(Jan 07 2020 at 21:09)</a>:</h4>
<p>However you can do better since you know the main thread is suspended while the signal handler is running:</p>
<div class="codehilite"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">DATA</span>: <span class="kt">i64</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="k">static</span><span class="w"> </span><span class="n">FLAG</span>: <span class="nc">AtomicBool</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">AtomicBool</span>::<span class="n">new</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">sig_handler</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Since the main thread is suspended, the</span>
<span class="w">    </span><span class="c1">// entire signal handler is an atomic operation</span>
<span class="w">    </span><span class="c1">// from the point of view of the thread.</span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">DATA</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">FLAG</span><span class="p">.</span><span class="n">store</span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="n">Ordering</span>::<span class="n">Relaxed</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">main_thread</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Acquire ensures the load instruction for FLAG happens before the load instruction for DATA</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">FLAG</span><span class="p">.</span><span class="n">load</span><span class="p">(</span><span class="n">Ordering</span>::<span class="n">Relaxed</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">compiler_fence</span><span class="p">(</span><span class="n">Ordering</span>::<span class="n">Acquire</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">DATA</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="185055156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185055156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185055156">(Jan 07 2020 at 21:10)</a>:</h4>
<p>does the Acquire need a compiler_fence Release to match it?</p>



<a name="185055176"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185055176" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185055176">(Jan 07 2020 at 21:10)</a>:</h4>
<p>Not in this case because there is an implicit one when the signal handler ends.</p>



<a name="185055234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185055234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185055234">(Jan 07 2020 at 21:11)</a>:</h4>
<p>Or to put it another way, it doesn't matter if FLAG is stored before DATA because the thread is suspended by the signal handler.</p>



<a name="185055249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185055249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185055249">(Jan 07 2020 at 21:11)</a>:</h4>
<p>So from the thread's point of view they both happen at the same time.</p>



<a name="185055318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185055318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185055318">(Jan 07 2020 at 21:12)</a>:</h4>
<p>i meant in the main thread, it doesn't need a release to match the acquire?</p>



<a name="185055368"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185055368" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185055368">(Jan 07 2020 at 21:12)</a>:</h4>
<p>the ordering docs made it sound like you should always use matched pairs</p>



<a name="185055403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185055403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185055403">(Jan 07 2020 at 21:13)</a>:</h4>
<p>You mean in the first example? The Release in the signal handler is matched with the Acquire in the main thread.</p>



<a name="185056232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185056232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185056232">(Jan 07 2020 at 21:23)</a>:</h4>
<p>In this one:</p>
<div class="codehilite"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">DATA</span>: <span class="kt">i64</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="k">static</span><span class="w"> </span><span class="n">FLAG</span>: <span class="nc">AtomicBool</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">AtomicBool</span>::<span class="n">new</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">sig_handler</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Since the main thread is suspended, the</span>
<span class="w">    </span><span class="c1">// entire signal handler is an atomic operation</span>
<span class="w">    </span><span class="c1">// from the point of view of the thread.</span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">DATA</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">FLAG</span><span class="p">.</span><span class="n">store</span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="n">Ordering</span>::<span class="n">Relaxed</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">main_thread</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Acquire ensures the load instruction for FLAG happens before the load instruction for DATA</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">FLAG</span><span class="p">.</span><span class="n">load</span><span class="p">(</span><span class="n">Ordering</span>::<span class="n">Relaxed</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">compiler_fence</span><span class="p">(</span><span class="n">Ordering</span>::<span class="n">Acquire</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">DATA</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p><code>main_thread</code> has Acquire but no Release</p>



<a name="185056327"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185056327" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185056327">(Jan 07 2020 at 21:24)</a>:</h4>
<p>if that's fine it's no problem, but the docs for the atomic Ordering values make it <em>sound like</em> Acquire and Release must always have matching pairs</p>



<a name="185058602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185058602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185058602">(Jan 07 2020 at 21:50)</a>:</h4>
<p>Well, that is true in the general case.</p>



<a name="185058621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185058621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185058621">(Jan 07 2020 at 21:50)</a>:</h4>
<p>For example this code:</p>
<div class="codehilite"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">DATA</span>: <span class="kt">i64</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="k">static</span><span class="w"> </span><span class="n">FLAG</span>: <span class="nc">AtomicBool</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">AtomicBool</span>::<span class="n">new</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">thread_a</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Since the main thread is suspended, the</span>
<span class="w">    </span><span class="c1">// entire signal handler is an atomic operation</span>
<span class="w">    </span><span class="c1">// from the point of view of the thread.</span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">DATA</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">compiler_fence</span><span class="p">(</span><span class="n">Ordering</span>::<span class="n">Release</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">FLAG</span><span class="p">.</span><span class="n">store</span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="n">Ordering</span>::<span class="n">Relaxed</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">thread_b</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Acquire ensures the load instruction for FLAG happens before the load instruction for DATA</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">FLAG</span><span class="p">.</span><span class="n">load</span><span class="p">(</span><span class="n">Ordering</span>::<span class="n">Relaxed</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">compiler_fence</span><span class="p">(</span><span class="n">Ordering</span>::<span class="n">Acquire</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">DATA</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="185058710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185058710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185058710">(Jan 07 2020 at 21:52)</a>:</h4>
<p>Will work correctly on a uniprocessor system that simulates multiprocessing with preemption.</p>



<a name="185058829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185058829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185058829">(Jan 07 2020 at 21:53)</a>:</h4>
<p>But that's because you have 2 threads running and execution could switch to the other at any time.</p>



<a name="185058906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185058906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185058906">(Jan 07 2020 at 21:54)</a>:</h4>
<p>though if you use <code>thread::spawn</code> this will still be UB, uniprocessor or not. so, "work correctly" is relative.</p>



<a name="185058948"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185058948" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185058948">(Jan 07 2020 at 21:54)</a>:</h4>
<p>(assuming you actually mean threads here. the comments sound more like signal handlers.)</p>



<a name="185059015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185059015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185059015">(Jan 07 2020 at 21:55)</a>:</h4>
<p>I'm pretty sure my code will work on an uniprocessor system.</p>



<a name="185059137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185059137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185059137">(Jan 07 2020 at 21:56)</a>:</h4>
<p>Since you can think of the whole system only having a single thread, and task switching being done in a signal handler.</p>



<a name="185059335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185059335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185059335">(Jan 07 2020 at 21:58)</a>:</h4>
<p>if it was assembly I'd agree</p>



<a name="185059345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185059345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185059345">(Jan 07 2020 at 21:58)</a>:</h4>
<p>but it has UB in Rust, so none of these arguments matter</p>



<a name="185059371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185059371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185059371">(Jan 07 2020 at 21:58)</a>:</h4>
<p>this is the same argument as in <a href="https://www.ralfj.de/blog/2019/07/14/uninit.html" target="_blank" title="https://www.ralfj.de/blog/2019/07/14/uninit.html">this blog post</a></p>



<a name="185059405"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185059405" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185059405">(Jan 07 2020 at 21:59)</a>:</h4>
<p>the compiler is totally allowed to optimize your code to garbage because of the data race it has</p>



<a name="185059593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185059593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185059593">(Jan 07 2020 at 22:01)</a>:</h4>
<p>I may sound like a broken record, but I think this is an important point, which is why I keep making it^^. You can only apply hardware/OS-level reasoning after you established that the program doesn't have UB.</p>



<a name="185060815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185060815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185060815">(Jan 07 2020 at 22:17)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> But you agree the code is correct if I use <code>fence</code> instead of <code>compiler_fence</code>?</p>



<a name="185061284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185061284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185061284">(Jan 07 2020 at 22:23)</a>:</h4>
<p>i would like to note that a signal handler can also be triggered on any thread, not just the main thread</p>



<a name="185090926"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal%20handlers%20and%20Rust/near/185090926" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Signal.20handlers.20and.20Rust.html#185090926">(Jan 08 2020 at 09:04)</a>:</h4>
<p><span class="user-mention" data-user-id="143274">@Amanieu</span> yes. (Assuming all <code>main</code> does is just run those two functions in parallel.)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>